home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / LANG / C / GCC / V2-4-5 / GPPLIBSR00 / cc / indstream < prev    next >
Text File  |  1993-12-08  |  3KB  |  109 lines

  1. //    This is part of the iostream library, providing -*- C++ -*- input/output.
  2. //    Copyright (C) 1992 Per Bothner.
  3. //
  4. //    This library is free software; you can redistribute it and/or
  5. //    modify it under the terms of the GNU Library General Public
  6. //    License as published by the Free Software Foundation; either
  7. //    version 2 of the License, or (at your option) any later version.
  8. //
  9. //    This library is distributed in the hope that it will be useful,
  10. //    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. //    Library General Public License for more details.
  13. //
  14. //    You should have received a copy of the GNU Library General Public
  15. //    License along with this library; if not, write to the Free
  16. //    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18.  
  19. #ifdef __GNUG__
  20. #pragma implementation "indstream.h"
  21. #endif
  22. #include "indstream.h"
  23.  
  24. indirectbuf::indirectbuf(streambuf *get, streambuf *put, int delete_mode)
  25. : streambuf()
  26. {
  27.     _get_stream = get;
  28.     _put_stream = put == NULL ? get : put;
  29.     _delete_flags = delete_mode;
  30. }
  31.  
  32. indirectbuf::~indirectbuf()
  33. {
  34.     if (_delete_flags & ios::in)  delete get_stream();
  35.     if (_delete_flags & ios::out)  delete put_stream();
  36. }
  37.  
  38. int indirectbuf::xsputn(const char* s, int n)
  39. {
  40.     return put_stream()->sputn(s, n);
  41. }
  42.  
  43. int indirectbuf::xsgetn(char* s, int n)
  44. {
  45.     return get_stream()->sgetn(s, n);
  46. }
  47.  
  48. int indirectbuf::overflow(int c /* = EOF */)
  49. {
  50.     if (c == EOF)
  51.     return put_stream()->overflow(c);
  52.     else
  53.     return put_stream()->sputc(c);
  54. }
  55.  
  56. int indirectbuf::underflow()
  57. {
  58.     return get_stream()->sbumpc();
  59. }
  60.  
  61. streampos indirectbuf::seekoff(streamoff off, _seek_dir dir, int mode)
  62. {
  63.     int ret_val = 0;
  64.     int select = mode == 0 ? (ios::in|ios::out) : mode;
  65.     streambuf *gbuf = (select & ios::in) ? get_stream() : NULL;
  66.     streambuf *pbuf = (select & ios::out) ? put_stream() : NULL;
  67.     if (gbuf == pbuf)
  68.     ret_val = gbuf->seekoff(off, dir, mode);
  69.     else {
  70.     if (gbuf)
  71.         ret_val = gbuf->seekoff(off, dir, ios::in);
  72.     if (pbuf && ret_val != EOF)
  73.         ret_val = pbuf->seekoff(off, dir, ios::out);
  74.     }
  75.     return ret_val;
  76. }
  77.  
  78. streampos indirectbuf::seekpos(streampos pos, int mode)
  79. {
  80.     int ret_val = EOF;
  81.     int select = mode == 0 ? (ios::in|ios::out) : mode;
  82.     streambuf *gbuf = (select & ios::in) ? get_stream() : NULL;
  83.     streambuf *pbuf = (select & ios::out) ? put_stream() : NULL;
  84.     if (gbuf == pbuf)
  85.     ret_val = gbuf->seekpos(pos, mode);
  86.     else {
  87.     if (gbuf)
  88.         ret_val = gbuf->seekpos(pos, ios::in);
  89.     if (pbuf && ret_val != EOF)
  90.         ret_val = pbuf->seekpos(pos, ios::out);
  91.     }
  92.     return ret_val;
  93. }
  94.  
  95. int indirectbuf::sync()
  96. {
  97.     streambuf *gbuf = get_stream();
  98.     int ret_val = gbuf->sync();
  99.     if (ret_val == EOF) return ret_val;
  100.     streambuf *pbuf = put_stream();
  101.     if (pbuf != gbuf) return pbuf->sync();
  102.     else return ret_val;
  103. }
  104.  
  105. int indirectbuf::pbackfail(int c)
  106. {
  107.     return get_stream()->sputbackc(c);
  108. }
  109.